import axios from 'axios'
import Element from 'element-ui'
axios.defaults.baseURL = "http://47.94.205.85:8081"
import store from '../store/index.js'
// http://localhost:8088/
// 前置拦截
axios.interceptors.request.use(config => {
	store.state.Authorization = store.state.Authorization ?? ''
	if (store.state.Authorization != '' ) {
		config.headers.Authorization=store.state.Authorization
	}
	console.log(config.headers)
	return config
})

axios.interceptors.response.use(response => {
		let res = response.data;

		// console.log("=================")
		// console.log(res)
		// console.log("=================")
		// this.$vuex.commit('saverouter', to)
		// console.log(response)
		if (response.config.url == '/login') {
			store.commit('saveAuthorization', response.headers.authorization)
			localStorage.setItem('Authorization', response.headers.authorization)          
		}
		if (res.code === "1111") {
			return response
		} else {

			Element.Message.error('错了哦，这是一条错误消息', {
				duration: 3 * 1000
			})

			return Promise.reject(response.data.msg)
		}
	},
	error => {
		console.log(error)
		if (error.response.data) {
			error.message = error.response.data.msg
		}

        if(error.response.status === 500){
			return
		}
		if (error.response.status === 401) {
			// store.commit("REMOVE_INFO")
			// router.push("/login")
		}

		Element.Message.error(error.message, {
			duration: 3 * 1000
		})
		return Promise.reject(error)
	}
)
